﻿namespace MicroCloud.Identity.Database
{
    /// <summary>
    /// 用户角色信息数据实体映射配置
    /// </summary>
    public class UserRoleConfiguration : EntityTypeConfigurationBase<UserRole, long>
    {
        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<UserRole> builder)
        {
            builder.HasIndex(m => new { m.UserId, m.RoleId }).SetIndex("Ids", true);

            builder.HasOne(ur => ur.Role).WithMany(r => r.UserRoles).HasForeignKey(m => m.RoleId).SetForeignKey("RoleId");
            builder.HasOne(ur => ur.User).WithMany(u => u.UserRoles).HasForeignKey(m => m.UserId).SetForeignKey("UserId");

            builder.HasQueryFilter(o => o.Role.DeletedTime == null && o.User.DeletedTime == null);

            ConfigurationAppend(builder);
        }

    }
}
